package cn.edu.scau.cmi.liangzijian.adapter;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

import cn.edu.scau.cmi.liangzijian.adapter.studentSortAdapter.SortStudentAdapterOfClass;
import cn.edu.scau.cmi.liangzijian.adapter.studentSortAdapter.SortStudentAdapterOfObject;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;


public class AdapterUIController {

    @FXML private ResourceBundle resources;

    @FXML private URL location;

    @FXML private Button addButton1;

    @FXML private Button addButton2;

    @FXML private Button delButton1;

    @FXML private Button delButton2;

    @FXML private Button clearOutput1;
    
    @FXML private Button clearOutput2;
    
    @FXML private TextField id1;

    @FXML private TextField id2;

    @FXML private TextField name1;

    @FXML private TextField name2;

    @FXML private TextArea screen1;
    
    @FXML private TextArea screen2;

    @FXML  private Button sortButton1;

    @FXML private Button sortButton2;
    
    @FXML private Label tip1;

    @FXML private Label tip2;
    
    private StudentTable st1 = StudentTable.getInstance();
    private StudentTable st2 = StudentTable.getInstance();
    
    private SortStudentAdapterOfClass ssac = new SortStudentAdapterOfClass();
    private SortStudentAdapterOfObject ssao = new SortStudentAdapterOfObject();
    
    private Pattern intRegx = Pattern.compile("[\\d]{6}"); 
    
    boolean checkName(Label tip, String name) {
    	if(name.trim().isEmpty()) {
    		tip.setText("学号或姓名不能为空");
    		return false;
    	}
    	
    	if(name.contains(" ")) {
    		tip.setText("姓名不能包含空格");
    		return false;
    	}
    	
    	if(name.length() > 8) {
    		tip.setText("姓名长度不能大于8个字");
    		return false;
    	}
    	return true;
    }
   
    @FXML
    void add1(MouseEvent event) {
    	tip1.setText("");
    	
    	String idStr = id1.getText();
    	if(!idStr.isEmpty()) {
    		
    		if(!checkName(tip1, name1.getText()))
				return;
    		
    		if(intRegx.matcher(idStr).matches()) {
    			int id = Integer.parseInt(idStr);
        		
    			if(!st1.contains(id)){
        			String name = name1.getText();
        			Student newStudent = new Student(id, name);
        			st1.add(newStudent);
        			
        			screen1.appendText(newStudent.toString()+"\n");
        			
        			id1.setText("");
        			name1.setText("");
        		} else {
        			tip1.setText("该学生已存在");
        		}
    		} else {
    			tip1.setText("学号必须为6位整数");
    		}
    	} else {
    		tip1.setText("学号和姓名不能为空");
    	}
    }

    @FXML
    void add2(MouseEvent event) {
    	tip2.setText("");
    	
    	String idStr = id2.getText();
    	if(!idStr.isEmpty()) {
    		
    		if(!checkName(tip2, name2.getText()))
    			return ;
    		
    		if(intRegx.matcher(idStr).matches()) {
    			int id = Integer.parseInt(idStr);
        		
    			if(!st2.contains(id)){
        			String name = name2.getText();
        			Student newStudent = new Student(id, name);
        			st2.add(newStudent);
        			
        			screen2.appendText(newStudent.toString()+"\n");
        			
        			id2.setText("");
        			name2.setText("");
        		} else {
        			tip2.setText("该学生已存在");
        		}
    		} else {
    			tip2.setText("学号必须为6位整数");
    		}
    	} else {
    		tip2.setText("学号和姓名不能为空");
    	}
    }

    @FXML
    void del1(MouseEvent event) {
    	tip1.setText("");
    	
    	String idStr = id1.getText();
    	if(idStr != "") {
    		if(intRegx.matcher(idStr).matches()) {
	    		int id = Integer.parseInt(idStr);
	    		
	    		if(st1.contains(id)) {
	    			st1.remove(id);
	    			
	    			List<Student> students = st1.getStudents();
	    			retPrintStudentsInfo(screen1, students);
	    			
	    			id1.setText("");
        			name1.setText("");
	    		} else {
	    			tip1.setText("该学生不存在");
	    		}
    		}else {
    			tip1.setText("学号必须为6位整数");
    		}
    	} else {
    		tip1.setText("学号不能为空");
    	}
    }

    @FXML
    void del2(MouseEvent event) {
    	tip2.setText("");
    	
    	String idStr = id2.getText();
    	if(idStr != "") {
    		if(intRegx.matcher(idStr).matches()) {
	    		int id = Integer.parseInt(idStr);
	    		
	    		if(st2.contains(id)) {
	    			st2.remove(id);
	    			
	    			List<Student> students = st2.getStudents();
	    			retPrintStudentsInfo(screen2, students);
	    			
	    			id2.setText("");
        			name2.setText("");
	    		} else {
	    			tip2.setText("该学生不存在");
	    		}
    		}else {
    			tip2.setText("学号必须为6位整数");
    		}
    	} else {
    		tip2.setText("学号不能为空");
    	}
    }

    @FXML
    void sort1(MouseEvent event) {
    	ssac.sortStudent(st1.getStudents());
    	retPrintStudentsInfo(screen1, st1.getStudents());
    }

    @FXML
    void sort2(MouseEvent event) {
    	ssao.sortStudent(st2.getStudents());
    	retPrintStudentsInfo(screen2, st2.getStudents());
    }

    @FXML
    void clear1(MouseEvent event) {
    	tip1.setText("");
    	screen1.clear();
    }
    
    @FXML
    void clear2(MouseEvent event) {
    	tip2.setText("");
    	screen2.clear();
    }
    
    void retPrintStudentsInfo(TextArea screen, List<Student> students) {
    	screen.clear();
    	for(Student student : students) {
    		screen.appendText(student.toString()+"\n");
    	}
    }
    
    @FXML
    void initialize() {
    	assert addButton1 != null : "fx:id=\"addButton1\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert addButton2 != null : "fx:id=\"addButton2\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert clearOutput1 != null : "fx:id=\"clearOutput1\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert clearOutput2 != null : "fx:id=\"clearOutput2\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert delButton1 != null : "fx:id=\"delButton1\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert delButton2 != null : "fx:id=\"delButton2\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert id1 != null : "fx:id=\"id1\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert id2 != null : "fx:id=\"id2\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert name1 != null : "fx:id=\"name1\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert name2 != null : "fx:id=\"name2\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert screen1 != null : "fx:id=\"screen1\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert screen2 != null : "fx:id=\"screen2\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert sortButton1 != null : "fx:id=\"sortButton1\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert sortButton2 != null : "fx:id=\"sortButton2\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert tip1 != null : "fx:id=\"tip1\" was not injected: check your FXML file 'AdapterUI.fxml'.";
        assert tip2 != null : "fx:id=\"tip2\" was not injected: check your FXML file 'AdapterUI.fxml'.";
    }

}
